import {Component, OnInit} from '@angular/core';
import {ProjectPutService} from "../service/project.put.service";
import {Report} from "../entity/Report";
import {Project} from "../entity/Project";
import {Params, ActivatedRoute, Router} from "@angular/router";
import {ProjectFindService} from "../service/project.find.service";
import {ProjectAddService} from "../service/project.add.service";
import {Pattern} from "@angular/cli/plugins/glob-copy-webpack-plugin";

@Component({
  selector: 'mini-point-component',
  templateUrl: './mini-point.html',
  styleUrls: ['./project-index-settings.css']
})
export class MiNiPointComponent implements OnInit{
  public project:Project;

  ngOnInit(): void {
    this.activatedRouter.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.projectService.getProjectInfo(params["id"]))
      .subscribe((project: Project) =>
        {
          this.project = project;
        }
      );
  }
  constructor(private projectPutService:ProjectPutService,
              private projectService: ProjectFindService,
              private projectAddService:ProjectAddService,
              private activatedRouter: ActivatedRoute,
              private router: Router){

  }
  public point(id:string){
    var email=sessionStorage.getItem("email");
    if(email==null||email==''){
      $.jBox.tip("请登录！");
      this.HiddenLeftProgram();
      this.router.navigate(['/isLogin']);
       return;
    }
    let userId  = sessionStorage.getItem("id");
    this.projectPutService.pointProject(id,userId)
      .then();
    $.jBox.tip("点赞完成！");

  }
  public HiddenLeftProgram(){
    document.getElementById("leftCatalog").removeAttribute("style");
    document.getElementById("leftCatalog").setAttribute("style","display:none");
    document.getElementById("nav").removeAttribute("style");
    document.getElementById("nav").setAttribute("style","display:none");
    document.getElementById("header").removeAttribute("style");
    document.getElementById("header").setAttribute("style","display:none");
  }
  public follow(id:string){
    var email=sessionStorage.getItem("email");
    let userId  = sessionStorage.getItem("id");
    if(email==null||email=='') {
      $.jBox.tip("请登录！");
      this.HiddenLeftProgram();
    this.router.navigate(['/isLogin']);
     return;
    }
    this.projectPutService.followProject(id,userId)
      .then();
    $.jBox.tip("关注成功！");
  }
  public report(id:string){
    let userId  = sessionStorage.getItem("id");
    this.box(id);
  }

  public box(id:string){
     let self = this;
    let boxContent = `
<div class="errorBlock" style="display: none;"></div>
 
<div id="box" style="padding: 20px;">

     <div> 举报标题:<input style="width: 100%;"  type="text" id="title" placeholder="请输入标题"/></div>
     <div> 联系方式:<input id="contact" style="width: 100%;" type="text" placeholder="请输入联系方式"/></div>
      <div>描述:<textarea id="description" style="width: 100%;" cols="4" rows="4" placeholder="请填写描述信息"/>
</div>
</div>`;
    var boxEvent = {

      title: "举报信息",
      width: 500,
      height: 300,
      buttons:{"确定":"ok","关闭":"cancel"},
      submit:function (v, h, f) {
        if(v=="cancel"){
          $.jBox.tip("cancel");
          return true;
        }
        if(v=="ok")
        {
          h.find('.errorBlock').hide('fast', function () { $(this).remove(); });


          var title = h.find('#title').val();
          var contact = h.find('#contact').val();
          var description = h.find('#description').val();
          if (title == '') {
            $('<div class="errorBlock" style="display: none;">请输入标题！</div>').prependTo(h).show('fast');
            return false;
          }

          if (contact == '') {
            $('<div class="errorBlock" style="display: none;">请输入联系方式！</div>').prependTo(h).show('fast');
            return false;
          }
          if (description == '') {
            $('<div class="errorBlock" style="display: none;">请输入描述信息！</div>').prependTo(h).show('fast');
            return false;
          }
          if(title.length>10){
            $('<div class="errorBlock" style="display: none;">标题字数限制在20字以内,请重新输入</div>').prependTo(h).show('fast');
            return false;
          }
          if(description.length>50){
            $('<div class="errorBlock" style="display: none;">举报内容字数为100字以内,请重新输入</div>').prependTo(h).show('fast');
            return false;
          }


           let report  = new Report();
          report.title=title;
          report.concat=contact;
          report.description=description;
          report.project.id=id;
          self.sendReport(report);
          // this.projectService.getProjectInfo("1");
          return true;
        }
      },loaded:function(h){
        $(".jbox-content",top.document).css("overflow-y","hidden");
      }

    };
    // boxEvent.projectAddService =  this.projectAddService;
    // $.jBox.projectAddService = this.projectAddService;
    $.jBox("html:"+boxContent, boxEvent);


  }


  sendReport(report: Report) {
    this.projectAddService.reportProject(report)
      .then();
  }
}
